package com.kichina.parking.controller.parking.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kichina.parking.controller.parking.model.NoLicenceInParkRequest;
import com.kichina.parking.controller.parking.model.RemoteOpenRequest;
import com.kichina.parking.controller.parking.model.WhiteListSyncRequest;
import com.kichina.parking.module.message.socket.SocketStorageDeal;
import com.kichina.parking.service.logic.bean.WhitelistInfoRecord;
import com.kichina.parking.service.logic.service.WhitelistInfoRecordService;
import com.kichina.parking.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Surging
 * @version 1.0
 * @ClassName CarBanDownContorlHandler
 * @date 2022-09-07 15:19
 * @desc 停车管理下行控制业务处理
 */
@Component
@Slf4j
public class CarBanDownControlHandler {

    public static ConcurrentHashMap<String, WhitelistInfoRecord> infoRecords = new ConcurrentHashMap<>();

    private final WhitelistInfoRecordService whitelistInfoRecordService;

    public CarBanDownControlHandler(WhitelistInfoRecordService whitelistInfoRecordService) {
        this.whitelistInfoRecordService = whitelistInfoRecordService;
    }

    /**
     * @param remoteOpenRequest
     * @author Surging
     * @Description
     * @Return void
     * @date 11:56 2022-09-21
     **/
    public void remoteOpen(RemoteOpenRequest remoteOpenRequest) {
        SocketStorageDeal.sendMessage(remoteOpenRequest.getParkid(), remoteOpenRequest);
    }

    /**
     * @param noLicenceInParkRequest
     * @author Surging
     * @Description
     * @Return void
     * @date 11:56 2022-09-21
     **/
    public void visitorAdmission(NoLicenceInParkRequest noLicenceInParkRequest) {
        SocketStorageDeal.sendMessage(noLicenceInParkRequest.getParkid(), noLicenceInParkRequest);
    }

    /**
     * @param whiteListSyncRequest
     * @author Surging
     * @Description 固定车注册与修改
     * @Return void
     * @date 11:57 2022-09-21
     **/
    public void remoteWhiteList(WhiteListSyncRequest whiteListSyncRequest) throws ParseException {

        WhitelistInfoRecord whitelistInfoRecord = new WhitelistInfoRecord();
        whitelistInfoRecord.setDealTag(false);
        if (Objects.nonNull(whiteListSyncRequest.getService())){
            whitelistInfoRecord.setService(whiteListSyncRequest.getService());
        }
        if (Objects.nonNull(whiteListSyncRequest.getUuid())){
            whitelistInfoRecord.setUuid(whiteListSyncRequest.getUuid());
        }
        if (Objects.nonNull(whiteListSyncRequest.getParkid())){
            whitelistInfoRecord.setParkid(whiteListSyncRequest.getParkid());
        }
        if (Objects.nonNull(whiteListSyncRequest.getCard_id())){
            whitelistInfoRecord.setCardId(whiteListSyncRequest.getCard_id());
        }
        if(Objects.nonNull(whiteListSyncRequest.getCar_number())){
            whitelistInfoRecord.setCarNumber(whiteListSyncRequest.getCar_number());
        }
        if(Objects.nonNull(whiteListSyncRequest.getCar_type())){
            whitelistInfoRecord.setCarType(whiteListSyncRequest.getCar_type());
        }
        if(Objects.nonNull(whiteListSyncRequest.getCard_type())){
            whitelistInfoRecord.setCardType(whiteListSyncRequest.getCard_type());
        }
        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        if (Objects.nonNull(whiteListSyncRequest.getStartdate())){
            whitelistInfoRecord.setStartdate(whiteListSyncRequest.getStartdate());
            //字符串格式两种 2022-10-15; 2022-09-16 15:54:00;
            if(String.valueOf(whiteListSyncRequest.getStartdate()).length()<15){
                whitelistInfoRecord.setStartdateDate(simpleDateFormat2.parse(String.valueOf(whiteListSyncRequest.getStartdate())));
            } else {
                whitelistInfoRecord.setStartdateDate(simpleDateFormat.parse(String.valueOf(whiteListSyncRequest.getStartdate())));
            }
        }
        if (Objects.nonNull(whiteListSyncRequest.getValiddate())) {
            whitelistInfoRecord.setValiddate(whiteListSyncRequest.getValiddate());
            if(String.valueOf(whiteListSyncRequest.getValiddate()).length()<15){
                whitelistInfoRecord.setValiddateDate(simpleDateFormat2.parse(String.valueOf(whiteListSyncRequest.getValiddate())));
            }else {
                whitelistInfoRecord.setValiddateDate(simpleDateFormat.parse(String.valueOf(whiteListSyncRequest.getValiddate())));
            }
        }
        if (Objects.nonNull(whiteListSyncRequest.getCreate_time())) {
            whitelistInfoRecord.setCreateTime(whiteListSyncRequest.getCreate_time());
            if(String.valueOf(whiteListSyncRequest.getCreate_time()).length()<15){
                whitelistInfoRecord.setCreateTimeDate(simpleDateFormat2.parse(String.valueOf(whiteListSyncRequest.getCreate_time())));
            }else {
                whitelistInfoRecord.setCreateTimeDate(simpleDateFormat.parse(String.valueOf(whiteListSyncRequest.getCreate_time())));
            }
        }
        if (Objects.nonNull(whiteListSyncRequest.getModify_time())) {
            whitelistInfoRecord.setModifyTime(whiteListSyncRequest.getModify_time());
            if(String.valueOf(whiteListSyncRequest.getModify_time()).length()<15){
                whitelistInfoRecord.setModifyTimeDate(simpleDateFormat2.parse(String.valueOf(whiteListSyncRequest.getModify_time())));
            } else {
                whitelistInfoRecord.setModifyTimeDate(simpleDateFormat.parse(String.valueOf(whiteListSyncRequest.getModify_time())));
            }
        }
        if(Objects.nonNull(whiteListSyncRequest.getCardmoney())){
            whitelistInfoRecord.setCardmoney(BigDecimal.valueOf(whiteListSyncRequest.getCardmoney()));
        }
        if(Objects.nonNull(whiteListSyncRequest.getPeriod())){
            whitelistInfoRecord.setPeriod(whiteListSyncRequest.getPeriod());
        }
        if(Objects.nonNull(whiteListSyncRequest.getCarusername())){
            whitelistInfoRecord.setCarusername(whiteListSyncRequest.getCarusername());
        }
        if(Objects.nonNull(whiteListSyncRequest.getCarusertel())){
            whitelistInfoRecord.setCarusertel(whiteListSyncRequest.getCarusertel());
        }
        if(Objects.nonNull(whiteListSyncRequest.getDrive_no())){
            whitelistInfoRecord.setDriveNo(whiteListSyncRequest.getDrive_no());
        }
        if(Objects.nonNull(whiteListSyncRequest.getAddress())){
            whitelistInfoRecord.setAddress(whiteListSyncRequest.getAddress());
        }
        if(Objects.nonNull(whiteListSyncRequest.getCarlocate())){
            whitelistInfoRecord.setCarlocate(whiteListSyncRequest.getCarlocate());
        }
        if(Objects.nonNull(whiteListSyncRequest.getHasmoreplate())){
            whitelistInfoRecord.setHasmoreplate(whiteListSyncRequest.getHasmoreplate());
        }
        if(Objects.nonNull(whiteListSyncRequest.getZmcarlocatecount())){
            whitelistInfoRecord.setZmcarlocatecount(whiteListSyncRequest.getZmcarlocatecount());
        }
        if(Objects.nonNull(whiteListSyncRequest.getZmcarinfo())){
            whitelistInfoRecord.setZmcarinfo(whiteListSyncRequest.getZmcarinfo());
        }
        if(Objects.nonNull(whiteListSyncRequest.getOperator())){
            whitelistInfoRecord.setOperator(whiteListSyncRequest.getOperator());
        }
        if(Objects.nonNull(whiteListSyncRequest.getLimitdaytype())){
            whitelistInfoRecord.setLimitDayType(whiteListSyncRequest.getLimitdaytype());
        }
        if(Objects.nonNull(whiteListSyncRequest.getOperate_type())){
            whitelistInfoRecord.setOperateType(whiteListSyncRequest.getOperate_type());
        }
        if(Objects.nonNull(whiteListSyncRequest.getRemark())){
            whitelistInfoRecord.setRemark(whiteListSyncRequest.getRemark());
        }
        if(1==whiteListSyncRequest.getOperate_type()){
            //添加
            whitelistInfoRecord.setWhitelistInfoRecordId(UUIDUtil.getUUID());
            whitelistInfoRecord.setServiceCreateTime(new Date());
            whitelistInfoRecord.setUpdateTime(new Date());
            whitelistInfoRecord.insert();
        }else if(3==whiteListSyncRequest.getOperate_type()) {
            whitelistInfoRecord.setUpdateTime(new Date());
            if (Objects.nonNull(whiteListSyncRequest.getWhitelistInfoRecordId())){
                whitelistInfoRecord.setWhitelistInfoRecordId(whiteListSyncRequest.getWhitelistInfoRecordId());
                whitelistInfoRecordService.updateById(whitelistInfoRecord);
                whiteListSyncRequest.setWhitelistInfoRecordId(null);
            }else {
                whitelistInfoRecordService.update(whitelistInfoRecord,
                        new QueryWrapper<WhitelistInfoRecord>()
                                .lambda().eq(WhitelistInfoRecord ::getParkid,whitelistInfoRecord.getParkid())
                                .eq(WhitelistInfoRecord ::getCarNumber, whitelistInfoRecord.getCarNumber()));

            }
        }else {
            whitelistInfoRecord.setUpdateTime(new Date());
            if (Objects.nonNull(whiteListSyncRequest.getWhitelistInfoRecordId())){
                whitelistInfoRecord.setWhitelistInfoRecordId(whiteListSyncRequest.getWhitelistInfoRecordId());
                infoRecords.put(whiteListSyncRequest.getUuid(), whitelistInfoRecord);
                whiteListSyncRequest.setWhitelistInfoRecordId(null);
            }else {
                infoRecords.put(whiteListSyncRequest.getUuid(), whitelistInfoRecord);
            }

        }
        SocketStorageDeal.sendMessage(whiteListSyncRequest.getParkid(), whiteListSyncRequest);
    }
}
